package com.kuang.controller;

import com.kuang.service.UserService;
import com.kuang.springcloud.pojo.User;
import com.kuang.springcloud.pojo.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
//@Controller
public class UserController {
/**
 * @description：TODO

 * @author     ：LK
 * @date       ：Created in 2021-10-26 15:11
 */
@Autowired
 UserService userService;
//获取微服务的东西
    @Autowired
  private DiscoveryClient discoveryClient;
//@ResponseBody
@RequestMapping("/user/find")
public List<Userinfo> findAllcontroller(){
    return userService.findAll();
}
//    @ResponseBody
    @RequestMapping("/user/get/{name}")
    public Userinfo getuserByUsernamecontroller(@PathVariable("name")String username){
        return userService.getuserByUsername(username);
    }
    @RequestMapping("/user/find/{username}")
    public Userinfo getuserByUsername(@PathVariable("username")String username){
        return userService.getuserByUsername(username);
    }
//    @RequestMapping("/user/find")
//    public List<Userinfo> updateWhole(){
//        return userService.updateWhole();
//    };

//    获取微服务的一些信息
    @RequestMapping("/user/discover")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        System.out.println("discover-->service"+userService);
        List<ServiceInstance> instances = discoveryClient.getInstances("springapplication_Eureka");
        for (ServiceInstance instant:instances) {
            System.out.println(
                    instant.getHost() + "\t" +
                    instant.getPort() + "\t" +
                    instant.getUri() + "\t" +
                    instant.getServiceId()
            );
        }
        return this.discoveryClient;
        }



//        redis测试的一些东西
@RequestMapping("/user/findreids/{username}")
public Userinfo getuserByUsernameRedis(@PathVariable("username")String username){
    return userService.getuserByUsernameRedis(username);
}



    @Autowired
    private RedisTemplate redisTemplate;
@RequestMapping("/Mysql2Postgre/getredis2postgre")
    public String testmysql2postgre(String username){
    /*
     *
     *  mysql准换到postgre中转换策略：
     *  把mysql中的数据取出来，放在redis中，设置key值，然后把redis中的数据放在postgre中
     *
     * */

    //然后把redis中的数据取出来
    ValueOperations<String,User> valueOperations = redisTemplate.opsForValue();
    User user = valueOperations.get(username);
    System.out.println("从缓存中获取数据:"+user.getAnswer());

    // 放在postgre中

//    int adduser = userService.adduser();

    return "lk牛逼大了！";
    }

    }
